package com.admin.education.controllers.business;

import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.jwt.JwtAuthorize;
import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.response.ResponseMessage;
import com.admin.education.common.response.ResponseMessageWithData;
import com.admin.education.common.response.Result;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.userInformation.Request.QueryUserInformationRequest;
import com.admin.education.dataTransferObject.userInformation.UserInformationTransfer;
import com.admin.education.services.Interfaces.IUserInformationService;
import com.admin.education.services.Interfaces.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.concurrent.Callable;

@JwtAuthorize
@RestController(value = "adminUserInformationController")
@RequestMapping("userInformations")
@CrossOrigin(origins = "*", maxAge = 3600)
public class UserInformationController {
    @Autowired
    private IUserInformationService userInformationService;
    @Autowired
    private IUserService userService;
    @GetMapping("")
    public Callable<IResponseMessage> queryUserInformation(@Valid QueryUserInformationRequest queryUserInformationRequest){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<PageResponse<UserInformationTransfer>> result = userInformationService.queryUserInformation(queryUserInformationRequest);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<PageResponse<UserInformationTransfer>>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PostMapping("{userInformationId}/Pass")
    public Callable<IResponseMessage> pass(@PathVariable Long userInformationId){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = userInformationService.auditUserInformation(userInformationId,true);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @PostMapping("{userInformationId}/NoPass")
    public Callable<IResponseMessage> noPass(@PathVariable Long userInformationId){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult result = userInformationService.auditUserInformation(userInformationId,false);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessage(Result.Success);
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
    @GetMapping("{userInformationId}")
    public Callable<IResponseMessage> queryUserInformationById(@PathVariable Long userInformationId){
        return new Callable<IResponseMessage>() {
            @Override
            public IResponseMessage call() throws Exception {
                IResponseMessage responseMessage = null;

                ServicesExcuteResult<UserInformationTransfer> result = userService.getCurrentInformation(userInformationId);
                if(result.isSuccess()){
                    responseMessage = new ResponseMessageWithData<UserInformationTransfer>(Result.Success,result.getData());
                }
                else{
                    throw new BadRequestException(result.getErrorMessage());
                }
                return responseMessage;
            }
        };
    }
}
